import requests

# 服务器地址
url = 'http://localhost:8080/stream'

try:
    # 发送请求，设置 stream 为 True 以流式接收响应
    response = requests.post(url, stream=True)
    # 检查响应状态码
    response.raise_for_status()

    # 逐行读取响应内容
    for line in response.iter_lines():
        if line:
            # 解码字节流为字符串
            line = line.decode('utf-8')
            if line.startswith('data:'):
                # 提取消息内容
                message = line[len('data:'):].strip()
                print(f"Received message: {message}")

except requests.exceptions.RequestException as e:
    print(f"Request error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
